using TeachlyQBank.DbPool;
using StackExchange.Redis;
using TeachlyQBank.DbServer;
using TeachlyQBank.Models;

var builder = WebApplication.CreateBuilder(args);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//配置所有的数据库连接并将其注册为服务
var configuration = builder.Configuration;  // 获取 IConfiguration

string mySqlConnectionString = configuration.GetConnectionString("MysqlConnection"); //获取MYSQL连接字符串
builder.Services.AddSingleton<MySqlConnectionAdapter>(new MySqlConnectionAdapter(mySqlConnectionString));  // 注册数据库连接服务

string oracleConnectionString = configuration.GetConnectionString("OracleConnection"); //获取Oracle连接字符串
builder.Services.AddSingleton<OracleConnectionAdapter>(new OracleConnectionAdapter(oracleConnectionString));  // 注册数据库连接服务

string sqlserverConnectionString = configuration.GetConnectionString("SQLServerConnection"); //获取SQLServer连接字符串
builder.Services.AddSingleton<SqlServerConnectionAdapter>(new SqlServerConnectionAdapter(sqlserverConnectionString));  // 注册数据库连接服务

string redisConnectionString = configuration.GetConnectionString("RedisConnection"); //获取Redis连接字符串
builder.Services.AddSingleton<IConnectionMultiplexer>(ConnectionMultiplexer.Connect(redisConnectionString));
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//在这里注册所有服务层的服务
builder.Services.AddHttpContextAccessor();                       // 注册 HttpContextAccessor
builder.Services.AddSingleton<UserServer>();                   //为控制器层注册UserServer
builder.Services.AddSingleton<KnowledgeServer>();        //为控制器层注册KnowledgeServer
builder.Services.AddSingleton<CourseServer>();               // 为控制器层注册CourseServer
builder.Services.AddSingleton<QuestionServer>();            // 为控制器层注册QuestionServer
builder.Services.AddSingleton<QuestionTypeServer>();    // 为控制器层注册QuestionTypeServer
builder.Services.AddSingleton<AnswerServer>();              // 为控制器层注册AnswerServer
builder.Services.AddSingleton<PowerServer>();                // 为控制器层注册:PowerServer
builder.Services.AddSingleton<RoleServer>();                   // 为控制器层注册:RoleServer
builder.Services.AddSingleton<PaperServer>();                //  为控制器层注册:PaperServer


builder.Services.AddSingleton<PermissionServer>();             //为控制器层注册：PermissionServer
builder.Services.AddSingleton<ExamServer>();                   // 为控制器层注册ExamServer

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

builder.Services.AddControllersWithViews();


var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}

app.UseRouting();                // 配置路由
app.UseHttpsRedirection();  // 将Http重定向到Https
//app.UseMiddleware<UserAuthenticationMiddleware>(); // 添加检验用户身份信息的中间件
app.UseStaticFiles();             // 提供静态文件如上面的css,html等
app.UseAuthorization();      // 处理授权

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=UserAndRole}/{action=Login}/{id?}");

app.Run();

